﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KPI.Controls
{
	public partial class News : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindData();
			}
		}

		private void BindData()
		{
			var newsRepository = new DataAccess.Repository.DynamicWorkRepository();

			var pageCount = 0;
			var rowCount = 0;
			var data = newsRepository.GetWorkNewsListPageing(null, 8, 1, out pageCount, out rowCount);

			if(data != null && data.Count > 0)
			{
				foreach(var item in data)
				{
					var addTime = item.WorkNewsAddTime.HasValue ? item.WorkNewsAddTime.Value.ToString("yyyy/MM/dd") : string.Empty;
					AddNewsItem(item.WorkNewsTitle, addTime, item.WorkNewsID.ToString());
				}
			}
			else
			{
				fsNews.Visible = false;
			}
		}

		private void AddNewsItem(string title, string addTime, string id)
		{
			if(!string.IsNullOrWhiteSpace(title) && !string.IsNullOrWhiteSpace(id))
			{
				System.Web.UI.HtmlControls.HtmlGenericControl item = new System.Web.UI.HtmlControls.HtmlGenericControl("div");

				var pdTitle = new System.Web.UI.HtmlControls.HtmlAnchor();
				var span = new System.Web.UI.HtmlControls.HtmlGenericControl("span");

				pdTitle.InnerText = title.Length > 23 ? title.Substring(0, 22) + "..." : title;
				pdTitle.HRef = "~/DynamicWorkDetail.aspx?wnid=" + id;
				pdTitle.Title = "点击查看明细";

				span.InnerText = addTime;
				span.Attributes.Add("class", "date");

				item.Controls.Add(pdTitle);
				item.Controls.Add(span);

				this.newscontainer.Controls.Add(item);
			}
		}
	}
}